﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Qplus.Commands;
using Qplus.Infrastructure;
using System.Text;
using System.Diagnostics;

namespace Qplus.Command
{
    public class UploadPhotoCommand : AbsHeaderHttpCommand, HttpInputStreamProcessor
    {
        const String COMMAND = "UploadPhoto";
	
	    const String FILENAME_HEAD = "filename";
	
	    String filename;

        public event EventHandler<MessageReceivedFromServer> UploadPhotoCommandReceivedFromHttpServer;

	    /**
	     * @param uid
	     * @param UKEY
	     */
	    public UploadPhotoCommand(String uid, String UKEY)
            : base(COMMAND, uid, UKEY)
        {
		    
	    }
	
	    /**
	     * @param uid
	     * @param UKEY
	     */
        public UploadPhotoCommand(long uid, byte[] UKEY, String filePath)
            : base(COMMAND, Convert.ToString(uid)/*uid.GetType().ToString(),*/, Encoding.UTF8.GetString(UKEY, 0, 32))
        {
		    this.filename = filePath;
	    }

        public HttpTask execute(String url)
        {
            base.MessageReceivedFromHttpServer += new EventHandler<MessageReceivedFromServer>(UploadPhotoCommand_MessageReceivedFromHttpServer);
            return base.execute(url, this);
        }

        void UploadPhotoCommand_MessageReceivedFromHttpServer(object sender, MessageReceivedFromServer e)
        {
            //throw new NotImplementedException();
            Debug.WriteLine("UploadPhotoCommand: Upload Photo File");
            ProcessIncomingMessage(e.strMessage);
        }
        private void ProcessIncomingMessage(string ReceivedMessage)
        {
            // Future hook for handling the message in somewhere else.
            // It's most probably wise to put the parsing logic in some other class.
            if (UploadPhotoCommandReceivedFromHttpServer != null)
                UploadPhotoCommandReceivedFromHttpServer(this, new MessageReceivedFromServer(ReceivedMessage));
        }
    }
}
